package com.luckdraw;

import java.util.ArrayList;
import java.util.List;

import org.nutz.ioc.Ioc;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;

import com.luckdraw.domain.Activity;
import com.luckdraw.domain.Codes;
import com.luckdraw.service.game.GamePoolRunnable;

public class MainSetup implements Setup {

    public void init(NutConfig conf) {
        Ioc ioc = conf.getIoc();
        org.nutz.dao.Dao dao = ioc.get(org.nutz.dao.Dao.class);
        org.nutz.dao.util.Daos.createTablesInPackage(dao, "com.luckdraw.domain", false);
        initData(dao);
        //启动活动队列线程
        GamePoolRunnable gamePoolRun = new GamePoolRunnable(ioc);
        new Thread(gamePoolRun).start();
        System.out.println("应用启动");
    }
    
    protected void initData(org.nutz.dao.Dao dao){
    	if(dao.count(Activity.class)==0){
    		List<Activity> acts = new ArrayList<Activity>();
    		Activity act1 = new Activity();
    		act1.setSize(4);
    		act1.setProfitable(2);
    		act1.setCost(100);
    		act1.setCommissionRate(0.2);
    		act1.setMinimumReturnRatio(0.1);
    		act1.setMaximumYieldRatio(1.0);
    		act1.setStatus(0);
    		acts.add(act1);
    		
    		Activity act3 = new Activity();
    		act3.setSize(4);
    		act3.setProfitable(2);
    		act3.setCost(300);
    		act3.setCommissionRate(0.2);
    		act3.setMinimumReturnRatio(0.1);
    		act3.setMaximumYieldRatio(1.0);
    		act3.setStatus(1);
    		acts.add(act3);
    		
    		Activity act5 = new Activity();
    		act5.setSize(4);
    		act5.setProfitable(2);
    		act5.setCost(500);
    		act5.setCommissionRate(0.2);
    		act5.setMinimumReturnRatio(0.1);
    		act5.setMaximumYieldRatio(1.0);
    		act5.setStatus(1);
    		acts.add(act5);
    		
    		Activity act10 = new Activity();
    		act10.setSize(4);
    		act10.setProfitable(2);
    		act10.setCost(1000);
    		act10.setCommissionRate(0.2);
    		act10.setMinimumReturnRatio(0.1);
    		act10.setMaximumYieldRatio(1.0);
    		act10.setStatus(1);
    		acts.add(act10);
    		dao.insert(acts);
    	}
    	if(dao.count(Codes.class)==0){
    		List<Codes> codes = new ArrayList<Codes>();
    		Codes code1 = new Codes();
    		code1.setKey("goods_body");
    		code1.setValue("欢喜过福年，拼手气分2亿");
    		code1.setDesc("欢喜过福年，拼手气分2亿");
    		codes.add(code1);

    		Codes code2 = new Codes();
    		code2.setKey("commission_rate");
    		code2.setValue("10");
    		code2.setDesc("推广分成比例%");
    		codes.add(code2);
    		
    		dao.insert(codes);
    	}
    }

    public void destroy(NutConfig conf) {
    	System.out.println("应用销毁");
    }
}